/* TEAM EXTREME EXCEL TOOL
 * A tool developed for Professor Bret Clarke (CS220)
 */

import teamExtreme.GuiFunctions;
import teamExtreme.GuiFunctions.ProjectGroups;
import teamExtreme.GuiFunctions.RandGroups;
import teamExtreme.GuiFunctions.RandList;
import teamExtreme.GuiFunctions.SoireeList;

import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.SystemColor;

public class Gui {
    private static JTextField textField;
    private static JTextField textField_1;
    public static void main(String[] args) {

    //initial pop-up dialog, prompt for master spreadsheet selection
    Object[] options = {"OK"};
    JOptionPane.showOptionDialog(null,
        "Please select your Master Spreadsheet. If one does not exist, you " +
        "may need to create one.",
        null,
        JOptionPane.DEFAULT_OPTION,
        JOptionPane.WARNING_MESSAGE, null, options, options[0]);

/****************************Main window frame*********************************/
    final JFrame frmTeamExtremeExcel = new JFrame("Team Extreme Excel Tool");
    frmTeamExtremeExcel.setAlwaysOnTop(true);
    frmTeamExtremeExcel.setResizable(false);
    frmTeamExtremeExcel.setVisible(true);
    frmTeamExtremeExcel.setSize(380, 320);
    frmTeamExtremeExcel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/******************************************************************************/

    JFileChooser fChooser = new JFileChooser();
    
    //keep looping until a valid spreadsheet file is selected
    boolean xlsFile = false;
    while(!xlsFile){
        //open file search window
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Excel Spreadsheet", "xls", "xlsx"
        );
        fChooser.setFileFilter(filter);
        int retVal = fChooser.showOpenDialog(frmTeamExtremeExcel);
        
        //if user cancels, quit
        if (retVal == fChooser.CANCEL_OPTION) {
            System.exit(0);
        }

        String masterName = fChooser.getSelectedFile().getName();
        if (masterName.contains(".xls"))
        {
            xlsFile = true;
        }
        else
        {
            JOptionPane.showOptionDialog(null,
                "An invalid spreadsheet file was selected. " + 
                "Please choose a different file.",
                "Not an Excel spreadsheet!",
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.WARNING_MESSAGE, null, options, options[0]);
        }
    }
    
    //this is where main program starts
    final GuiFunctions guiFunc = new GuiFunctions(fChooser.getSelectedFile());

/*********************************Tab panes************************************/
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        frmTeamExtremeExcel.getContentPane().add(tabbedPane,
                                                BorderLayout.CENTER);

/*****************************Tab1 Random List*********************************/
        
/****************************Tab2 Random Groups********************************/
        JPanel tab1 = new JPanel();
        tabbedPane.addTab("Random List", null, tab1, null);
        
                JButton tab1GenButton = new JButton("Generate Random List");
                tab1GenButton.setToolTipText("");
                tab1GenButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        RandList rList = guiFunc.new RandList();
                        rList.genRandList();
                    }
                });
                
                JTextPane txtpnClickToGenerate = new JTextPane();
                txtpnClickToGenerate.setBackground(SystemColor.menu);
                txtpnClickToGenerate.setEditable(false);
                txtpnClickToGenerate.setText("Click the button below to generate a random list of students.");
                GroupLayout gl_tab1 = new GroupLayout(tab1);
                gl_tab1.setHorizontalGroup(
                    gl_tab1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_tab1.createSequentialGroup()
                            .addContainerGap(21, Short.MAX_VALUE)
                            .addGroup(gl_tab1.createParallelGroup(Alignment.LEADING)
                                .addGroup(Alignment.TRAILING, gl_tab1.createSequentialGroup()
                                    .addComponent(txtpnClickToGenerate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18))
                                .addGroup(Alignment.TRAILING, gl_tab1.createSequentialGroup()
                                    .addComponent(tab1GenButton)
                                    .addGap(102))))
                );
                gl_tab1.setVerticalGroup(
                    gl_tab1.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_tab1.createSequentialGroup()
                            .addGap(91)
                            .addComponent(txtpnClickToGenerate, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(18)
                            .addComponent(tab1GenButton)
                            .addContainerGap(107, Short.MAX_VALUE))
                );
                tab1.setLayout(gl_tab1);
        JPanel tab2 = new JPanel();
        tabbedPane.addTab("Random Groups", null, tab2, null);
        
        final JComboBox comboBox = new JComboBox();
                
        JButton btnNewButton = new JButton("Generate Random Groups");
        btnNewButton.setToolTipText("");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //generate random groups
                int selected = Integer.parseInt(comboBox.getSelectedItem().toString());
                RandGroups rGroups = guiFunc.new RandGroups(selected);
                rGroups.genRandGroups();
            }
        });
                
        JLabel lblNewLabel_4 = new JLabel("Group Size:");
                
        
        comboBox.setMaximumRowCount(6);
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"3", "4", "5", "6"}));
        
        JTextPane txtpnSelectAGroup = new JTextPane();
        txtpnSelectAGroup.setBackground(SystemColor.menu);
        txtpnSelectAGroup.setEditable(false);
        txtpnSelectAGroup.setText("Select a group size. Then, click the button below to generate random groups of specified size.");
        GroupLayout gl_tab2 = new GroupLayout(tab2);
        gl_tab2.setHorizontalGroup(
            gl_tab2.createParallelGroup(Alignment.TRAILING)
                .addGroup(Alignment.LEADING, gl_tab2.createSequentialGroup()
                    .addGap(57)
                    .addComponent(txtpnSelectAGroup, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addGroup(gl_tab2.createSequentialGroup()
                    .addGap(70)
                    .addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(59))
                .addGroup(gl_tab2.createSequentialGroup()
                    .addContainerGap(127, Short.MAX_VALUE)
                    .addComponent(lblNewLabel_4)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(125))
        );
        gl_tab2.setVerticalGroup(
            gl_tab2.createParallelGroup(Alignment.LEADING)
                .addGroup(Alignment.TRAILING, gl_tab2.createSequentialGroup()
                    .addContainerGap(66, Short.MAX_VALUE)
                    .addComponent(txtpnSelectAGroup, GroupLayout.PREFERRED_SIZE, 48, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_tab2.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel_4)
                        .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addComponent(btnNewButton)
                    .addGap(75))
        );
        tab2.setLayout(gl_tab2);

        //tab3
/****************************Tab3 Soiree List**********************************/
        JPanel tab3 = new JPanel();
        tabbedPane.addTab("Soiree LIst", null, tab3, null);
        
        JButton btnNewButton_1 = new JButton("Generate Soiree List");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                SoireeList soireeList = guiFunc.new SoireeList();
                soireeList.genSoireeList();
            }
        });
        
        JTextPane txtpnClickTheButton = new JTextPane();
        txtpnClickTheButton.setBackground(SystemColor.menu);
        txtpnClickTheButton.setEditable(false);
        txtpnClickTheButton.setText("Click the button below to generate a random soiree list.");
        GroupLayout gl_tab3 = new GroupLayout(tab3);
        gl_tab3.setHorizontalGroup(
            gl_tab3.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_tab3.createSequentialGroup()
                    .addContainerGap(36, Short.MAX_VALUE)
                    .addGroup(gl_tab3.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_tab3.createSequentialGroup()
                            .addComponent(txtpnClickTheButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(26))
                        .addGroup(Alignment.TRAILING, gl_tab3.createSequentialGroup()
                            .addComponent(btnNewButton_1)
                            .addGap(107))))
        );
        gl_tab3.setVerticalGroup(
            gl_tab3.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_tab3.createSequentialGroup()
                    .addGap(105)
                    .addComponent(txtpnClickTheButton, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addComponent(btnNewButton_1)
                    .addContainerGap(97, Short.MAX_VALUE))
        );
        tab3.setLayout(gl_tab3);

/*************************Tab4 Random Project Groups***************************/
        //tab4
        JPanel tab4 = new JPanel();
        tabbedPane.addTab("Group List", null, tab4, null);

        textField = new JTextField();
        textField.setHorizontalAlignment(SwingConstants.RIGHT);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setHorizontalAlignment(SwingConstants.RIGHT);
        textField_1.setColumns(10);

        JLabel lblNewLabel = new JLabel("First name");

        JLabel lblNewLabel_1 = new JLabel("Second name");

        JButton btnCreateGroup = new JButton("Generate Group");
        btnCreateGroup.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //button clicked
                String n1 = textField.getText();
                String n2 = textField_1.getText();
                
                ProjectGroups pGroups = guiFunc.new ProjectGroups(n1, n2);
                if(!pGroups.getValid())
                {
                    Object[] options = {"OK"};
                    JOptionPane.showOptionDialog(null,
                            "The name(s) entered are invalid because they " +
                            "are already in a group, or they are not " +
                            "enrolled in the class.",
                            "Invalid Names",
                            JOptionPane.DEFAULT_OPTION,
                            JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                }
                else
                    pGroups.genProjectGroups();
            }
        });
        
        JTextPane txtpnEnterTwoStudent = new JTextPane();
        txtpnEnterTwoStudent.setBackground(SystemColor.menu);
        txtpnEnterTwoStudent.setEditable(false);
        txtpnEnterTwoStudent.setText("Enter two student names. Then, click the button below to generate a group of five with the two names included.");

        GroupLayout gl_tab4 = new GroupLayout(tab4);
        gl_tab4.setHorizontalGroup(
            gl_tab4.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_tab4.createSequentialGroup()
                    .addContainerGap(28, Short.MAX_VALUE)
                    .addGroup(gl_tab4.createParallelGroup(Alignment.LEADING)
                        .addGroup(Alignment.TRAILING, gl_tab4.createSequentialGroup()
                            .addGroup(gl_tab4.createParallelGroup(Alignment.LEADING)
                                .addComponent(lblNewLabel)
                                .addComponent(lblNewLabel_1))
                            .addGap(9)
                            .addGroup(gl_tab4.createParallelGroup(Alignment.LEADING, false)
                                .addComponent(textField, GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                                .addComponent(textField_1)
                                .addComponent(btnCreateGroup))
                            .addGap(41))
                        .addGroup(Alignment.TRAILING, gl_tab4.createSequentialGroup()
                            .addComponent(txtpnEnterTwoStudent, GroupLayout.PREFERRED_SIZE, 329, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
        );
        gl_tab4.setVerticalGroup(
            gl_tab4.createParallelGroup(Alignment.TRAILING)
                .addGroup(gl_tab4.createSequentialGroup()
                    .addContainerGap(67, Short.MAX_VALUE)
                    .addComponent(txtpnEnterTwoStudent, GroupLayout.PREFERRED_SIZE, 46, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addGroup(gl_tab4.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_tab4.createParallelGroup(Alignment.BASELINE)
                        .addComponent(lblNewLabel_1)
                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18)
                    .addComponent(btnCreateGroup)
                    .addGap(49))
        );
        tab4.setLayout(gl_tab4);
    }
}
